package com.qianfeng.smartdevice.controller;

import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.pojo.SysDevice;
import com.qianfeng.smartdevice.service.SysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by wenka on 2022/3/27 16:42
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/devices")
public class DeviceController {
    private SysDeviceService sysDeviceService;

    @Autowired
    public void setSysDeviceService(SysDeviceService sysDeviceService) {
        this.sysDeviceService = sysDeviceService;
    }

    @PostMapping("/device")
    public R addDevice(@RequestBody SysDevice sysDevice) {
        sysDeviceService.addSysDevice(sysDevice);
        return R.steOK();
    }

    @GetMapping("/device/{deviceId}")
    public R findById(@PathVariable String deviceId) {
        SysDevice sysDeviceServiceById = sysDeviceService.findById(deviceId);
        return R.setOK(sysDeviceServiceById);
    }

    @RequestMapping("/devices")
    public R findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "1") int limit, SysDevice sysDevice) {
        return R.setOK(sysDeviceService.findAll(page, limit, sysDevice));
    }
    @PostMapping("/command/{deviceId}/{command}")
    public R sendControl(@PathVariable String deviceId,@PathVariable String command){
        System.out.println("收到了向设备发送命令的操作了："+deviceId+"====>"+command);
        sysDeviceService.sendControl(deviceId, command);
        return R.steOK();
    }

}
